package br.unicamp.ic.mc906.kmeans.test;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Random;

import br.unicamp.ic.mc906.kmeans.KmeansInterface;

public class Point implements KmeansInterface<Point> {
	
	private double x;
	
	private double y;
	
	public Point() {}
	
	public Point(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	@Override
	public Point centroidOfCluster(List<Point> elements) {
		Point point = new Point();
		double x = 0.0;
		double y = 0.0;
		
		for (Point p : elements) {
			x += p.getX();
			y += p.getY();
		}
		
		x = x/elements.size();
		y = y/elements.size();
		point.setX(x);
		point.setY(y);
		
		return point;
	}
	
	@Override
	public double getDistance(Point element) {
		return Math.sqrt(Math.pow(this.x - element.getX(), 2)+Math.pow(this.y - element.getY(),2));
	}
	
	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(double y) {
		this.y = y;
	}
	
	public String toString() {
		NumberFormat nf = new DecimalFormat();
		nf.setMaximumFractionDigits(2);
		return "("+nf.format(this.x)+", "+nf.format(this.y)+")";
	}
	
	
}
